﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MpSoft.Library.DAO.Customers;
using MpSoft.Library.DTO;
using MpSoft.Basic.Configuration;

namespace MpSoft.Library.BUSS.Customers
{
    #region  PollAnswer
    public class PollAnswerBUSS
    {
        private const string POLLANSWER_ALL_KEY = "Mp.pollanswer.all-{0}";
        private const string POLLANSWER_BY_ID_KEY = "Mp.pollanswer.id-{0}";

        private static void RemoveCache()
        {
            MpCache.RemoveByPattern(POLLANSWER_ALL_KEY);
            MpCache.RemoveByPattern(POLLANSWER_BY_ID_KEY);
        }

        public static int PollAnswer_Add(PollAnswer obj)
        {
            var result = PollAnswerDAO.PollAnswer_Add(obj);
            if (result > 0)
                RemoveCache();
            return result;
        }
        public static int PollAnswer_Update(PollAnswer obj)
        {
            var result = PollAnswerDAO.PollAnswer_Update(obj);
            if (result > 0)
                RemoveCache();
            return result;
        }

        public static int PollAnswer_Delete(int PollAnswerID)
        {
            var result = PollAnswerDAO.PollAnswer_Delete(PollAnswerID);
            if (result > 0)
                RemoveCache();
            return result;
        }

        public static int PollAnswer_TotalCount(int PollID)
        {
            var list = PollAnswer_ByPollID(PollID);
            if (list == null)
                return 1;
            int total = 0;
            foreach (var obj in list)
                total += obj.Count;
            return total;
        }

        public static PollAnswer PollAnswer_ByID(int PollAnswerID)
        {
            var list = PollAnswer_List(PollAnswerID, -1);
            if (list != null)
                return list.FirstOrDefault();
            return null;
            
        }
        public static List<PollAnswer> PollAnswer_ByPollID(int PollID)
        {
            return PollAnswer_List(-1, PollID);

        }
        public static List<PollAnswer> PollAnswer_List(int PollAnswerID, int PollID)
        {
            return PollAnswerDAO.PollAnswer_List(PollAnswerID, PollID);
        }
    }
    #endregion
}
